<?php
$counterContent = '';
$base_url = $info['domain'];
// Default image style (font)
$style='vs';
// Default counter image extension
$default_ext = "gif";

$logfile  = 'modules/Counter/logs/index.log';
$logfile1 = 'modules/Counter/logs/history.log';
$style_folder = 'modules/Counter/counter_styles/' . $style . '/';
$ext = $default_ext;

$data = '';

if ( @$_SESSION['sescounter'] == '' )	//moi truy cap ==> count++
{
	// If the log file doesn't exist we start count from 1 ...
	if (! @$file = fopen($logfile,"r+"))
	{
		$count="1";
	}
	// If the log file exist lets read count from it
	else 
	{
		$data = @fread($file, filesize($logfile)) or $count=0;
		fclose($file);
		// Raise the value of $count by 1
		$temp = split(';',$data);	//[0]=ngay,[1] = soluong
		$count = floatval($temp[1]);
		$count++;
	}
	
	$_SESSION['sescounter'] = 'yes';
}
else	//refresh hoac chuyen trang
{
	// If the log file doesn't exist we start count from 1 ...
	if (! @$file = fopen($logfile,"r+"))
	{
		$count="1";
	}
	// If the log file exist lets read count from it
	else 
	{
		$data = @fread($file, filesize($logfile)) or $count=0;
		fclose($file);
		// Raise the value of $count by 1
		$temp = split(";",$data);	//[0]=ngay,[1] = soluong
		//print_r($temp);
		$count = floatval($temp[1]);
	}
}
// Write the new $count in the log file
$file = fopen($logfile,"w+") or die('Không đọc được file logs !');

//neu ngay hien tai != ngay trong counter -> them 1 dong moi tren dau file
//data -> du lieu cu
if ( date('d/m/Y',$temp[0]) != date('d/m/Y') )
{
	$file_history = fopen($logfile1,"r+") or die('Không đọc được file history !');
	$data1 = @fread($file_history, filesize($logfile1));
	fclose($file_history);
	//ghi vao file moi
	$fc = $temp[0] . ';' . ($count-1) . '#' . $data1;
	$file_history = fopen($logfile1,"w+") or die('Không đọc được file history !');
	fputs($file_history,$fc);
	fclose($file_history);
}
$fc = mktime(date('h,i,s,m,d,Y')) . ';' . $count;
fputs($file, $fc);
fclose($file);
/*for ($i=0;$i<strlen($count);$i++) {
    $digit=substr("$count",$i,1);
    // Build the image URL ...
    $src = $base_url . $style_folder . $digit . "." . $ext;
    //$counterContent .= '<img src=' . $src . ' border=0 align="absmiddle">';
    $counterContent .= $digit . '&nbsp;';
*/
$__vs_counter__ = $count;

?>